var e = require;
var t = module;
var o = exports;
var ii, nn = this && this.__extends || (ii = function (t, e) {
    return (ii = Object.setPrototypeOf || {
        __proto__: []
    }
        instanceof Array &&
        function (t, e) {
            t.__proto__ = e
        } ||
        function (t, e) {
            for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o])
        })(t, e)
},
    function (t, e) {
        function o() {
            this.constructor = t
        }
        ii(t, e),
            t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o)
    }
),
    rr = this && this.__awaiter ||
        function (t, e, o, i) {
            return new (o || (o = Promise))(function (n, a) {
                function r(t) {
                    try {
                        l(i.next(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function s(t) {
                    try {
                        l(i.
                            throw(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function l(t) {
                    var e;
                    t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
                        t(e)
                    })).then(r, s)
                }
                l((i = i.apply(t, e || [])).next())
            })
        };
var oo = (this && this.__decorate) ||
    function (e, t, a, n) {
        var i,
            o = arguments.length,
            s = o < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, a)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);
        else
            for (var r = e.length - 1; r >= 0; r--)
                (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
        return o > 3 && s && Object.defineProperty(t, a, s), s;
    }; Object.defineProperty(o, "__esModule", { value: !0 });
var a = e("BaseModuleUI"),
    n = e("NotifyEventType"),
    i = e("GlobalParams"),
    r = e("AudioManager"),
    s = e("CommonHelper"),
    l = e("PureHelper"),
    _ = e("Game"),
    c = e("node_team"),
    d = e("role_node"),
    h = e("team_node_info"),
    u = cc._decorator,
    p = u.ccclass,
    m = u.property,
    f = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.tower_team = _.TeamInfo.left_team),
                (t.tower_id = 0),
                (t.tower_ske = null),
                (t.hp_bar = null),
                (t.tower_map_node = null),
                (t.tower_boom = null),
                (t.role_solider = null),
                (t.attack_target = null),
                (t.tower_attack = !1),
                (t.tower_info = null),
                (t.tower_hp = 0),
                (t.tower_is_destroy = !1),
                (t.target_list = []),
                (t.tower_path_num = -1),
                t
            );
        }
        return (
            nn(t, e),
            (t.prototype.initView = function () {
                (this.tower_is_destroy = !1),
                    (this.tower_attack = !1),
                    (this.tower_ske.timeScale = 1),
                    this.getComponent(c.default).setTeamInfo(this.tower_team),
                    (this.role_solider = this.node.parent),
                    (this.hp_bar.width = 180),
                    (this.hp_bar.color =
                        this.tower_team ==
                            (i.GlobalParams.MatchMyIndex % 2 == 0 ? _.TeamInfo.left_team : _.TeamInfo.right_team)
                            ? new cc.Color(94, 189, 255)
                            : new cc.Color(250, 20, 20)),
                    (this.tower_info = _.default.tower_info[this.tower_id]),
                    (this.tower_hp = this.tower_info.hp),
                    this.showTowerAnime("idle", !0),
                    (this.tower_boom.node.active = !1),
                    this.showTowerScope(!1),
                    this.updateTime();
                var e = Number(this.node.name.substring(this.node.name.length - 1, this.node.name.length));
                e < 2
                    ? (this.tower_path_num = 0)
                    : e < 4
                        ? (this.tower_path_num = 1)
                        : e < 6 && (this.tower_path_num = 2);
            }),
            (t.prototype.updateTime = function () {
                var e = this;
                if (
                    i.GlobalParams.GameStart &&
                    (this.attack_target && cc.isValid(this.attack_target, !0)
                        ? !this.tower_attack && cc.isValid(this.attack_target, !0) && this.towerAttackTarget()
                        : this.getAttackTarget(),
                        !this.tower_is_destroy)
                ) {
                    var t = this.role_solider.getChildByName("role_team_" + i.GlobalParams.MatchMyIndex);
                    if (t && t.getComponent(c.default).getTeamInfo() != this.tower_team) {
                        var o = Math.abs(l.default.Distance(this.node.getPosition(), t.getPosition()));
                        this.showTowerScope(o < 450);
                    } else this.showTowerScope(!1);
                }
                this.scheduleOnce(function () {
                    e.updateTime();
                }, i.GlobalParams.GameUpdateTime);
            }),
            (t.prototype.getAttackTarget = function () {
                var e = 9999;
                this.attack_target = null;
                for (var t = 0; t < this.target_list.length; t++) {
                    var o = this.target_list[t];
                    if (o.active) {
                        var a = Math.abs(l.default.Distance(this.node.getPosition(), o.getPosition()));
                        e > a && ((e = a), (this.attack_target = o));
                    }
                }
            }),
            (t.prototype.towerAttackTarget = function () {
                var e = this;
                if (
                    !this.tower_is_destroy &&
                    ("role" == this.attack_target.group || "soldier" == this.attack_target.group)
                ) {
                    (this.tower_attack = !0), this.showTowerAnime("attack", !1);
                    var t = this.role_solider.getChildByName("role_team_" + i.GlobalParams.MatchMyIndex);
                    cc.isValid(t, !0) &&
                        Math.abs(l.default.Distance(this.node.getPosition(), t.getPosition())) < 400 &&
                        r.default.getInstance().playFx("tower_attack"),
                        this.tower_ske.setCompleteListener(function () {
                            if (
                                (e.tower_ske.setCompleteListener(null),
                                    e.showTowerAnime("idle", !0),
                                    e.attack_target && cc.isValid(e.attack_target, !0) && e.attack_target.active)
                            )
                                if (
                                    Math.abs(l.default.Distance(e.node.getPosition(), e.attack_target.getPosition())) >
                                    450
                                )
                                    e.attack_target = null;
                                else {
                                    "role" == e.attack_target.group &&
                                        e.attack_target.getComponent(d.default).roleBeHit(e.tower_info.attack, 10),
                                        "soldier" == e.attack_target.group &&
                                        e.attack_target
                                            .getComponent(h.default)
                                            .soldierBeHit(e.tower_info.attack, 10);
                                    var t = e.node.getPosition();
                                    e.emitEvent(n.default.SEND_TOWER_LIGHTING_TARGET_INFO, {
                                        start_pos: cc.v2(t.x, (t.y += 150)),
                                        target: e.attack_target
                                    });
                                }
                            cc.isValid(e.attack_target, !0) && !e.attack_target.active && (e.attack_target = null),
                                (e.tower_attack = !1);
                        });
                }
            }),
            (t.prototype.getTowerTargetInfo = function () {
                return this.attack_target;
            }),
            (t.prototype.towerBeHit = function (e, t) {
                var o = this;
                if (-1 != this.tower_path_num) {
                    if (
                        i.GlobalParams.GameDestroyTower[this.tower_team == _.TeamInfo.left_team ? 0 : 1][
                        this.tower_path_num
                        ] != this.tower_id
                    )
                        return;
                } else if (
                    -1 == i.GlobalParams.GameDestroyTower[this.tower_team == _.TeamInfo.left_team ? 0 : 1].indexOf(2)
                )
                    return;
                if (
                    !this.tower_is_destroy &&
                    ("idle" == this.tower_ske.animation &&
                        this.tower_ske.setCompleteListener(function () {
                            o.tower_ske.setCompleteListener(null), o.showTowerAnime("idle", !0);
                        }),
                        (this.tower_hp -= e),
                        (this.tower_hp = this.tower_hp < 0 ? 0 : this.tower_hp),
                        (this.hp_bar.width = (180 * this.tower_hp) / this.tower_info.hp),
                        this.emitEvent(n.default.SEND_SHOW_BE_HIT_INFO, {
                            attack: e,
                            my_team: this.tower_team == _.TeamInfo.left_team ? 0 : 1,
                            end_pos: cc.v2(this.node.getPosition().x, this.node.getPosition().y + 80)
                        }),
                        this.tower_hp <= 0 && !this.tower_is_destroy)
                ) {
                    (this.tower_is_destroy = !0),
                        i.GlobalParams.GameDestroyTower[this.tower_team == _.TeamInfo.left_team ? 0 : 1][
                        this.tower_path_num
                        ]++,
                        this.emitEvent(n.default.SEND_ROLE_BE_KILL_INFO, {
                            kill: t,
                            be_kill: _.KillType.tower,
                            team: this.tower_team
                        }),
                        (this.tower_ske.node.active = !1),
                        (this.hp_bar.parent.active = !1),
                        (this.tower_boom.node.active = !0),
                        this.tower_boom.setAnimation(0, "bz", !1);
                    var a = this.role_solider.getChildByName("role_team_" + i.GlobalParams.MatchMyIndex);
                    cc.isValid(a, !0) &&
                        Math.abs(l.default.Distance(this.node.getPosition(), a.getPosition())) < 700 &&
                        r.default.getInstance().playFx("tower_die"),
                        this.showTowerScope(!1),
                        this.tower_boom.setCompleteListener(function () {
                            o.tower_boom.setCompleteListener(null),
                                2 == o.tower_id
                                    ? ((i.GlobalParams.GameStart = !1),
                                        (i.GlobalParams.GamePlayerWin =
                                            o.tower_team !=
                                            (i.GlobalParams.MatchMyIndex % 2 == 0
                                                ? _.TeamInfo.left_team
                                                : _.TeamInfo.right_team)),
                                        o.emitEvent(n.default.SEND_GAME_OVER_INFO),
                                        o.node.destroy(),
                                        cc.log("游戏结束"))
                                    : o.node.destroy();
                        });
                }
            }),
            (t.prototype.onCollisionEnter = function (e, t) {
                var o = e.node;
                o.group, o.isValid && 1 == t.tag && 0 == e.tag && this.getSoliderInfo(o) && this.target_list.push(o);
            }),
            (t.prototype.onCollisionExit = function (e, t) {
                var o = e.node;
                if ((o.group, o.isValid && 1 == t.tag && 0 == e.tag && this.getSoliderInfo(o))) {
                    var a = this.target_list.indexOf(o);
                    this.target_list.splice(a, 1);
                }
            }),
            (t.prototype.getSoliderInfo = function (e) {
                var t = e.name;
                return (
                    t == "soldier_team_" + (this.tower_team == _.TeamInfo.left_team ? 2 : 1) ||
                    ("role_team" == t.substring(0, 9) &&
                        this.tower_team % 2 == Number(s.default.getNameType(t, "role_team_")) % 2)
                );
            }),
            (t.prototype.showTowerScope = function (e) {
                this.tower_map_node.getChildByName(this.node.name).active = e;
            }),
            (t.prototype.showTowerAnime = function (e, t) {
                var o = 2 == this.tower_id ? "sj_" : "t_";
                (o += this.tower_team == _.TeamInfo.right_team ? "h" : "l"),
                    "attack" == e && (o += "_gj"),
                    this.tower_ske.setAnimation(0, o, t);
            }),
            oo([m({ type: cc.Enum(_.TeamInfo), displayName: "防御塔阵容" })], t.prototype, "tower_team", void 0),
            oo([m({ type: cc.Float, displayName: "防御塔ID" })], t.prototype, "tower_id", void 0),
            oo([m(sp.Skeleton)], t.prototype, "tower_ske", void 0),
            oo([m(cc.Node)], t.prototype, "hp_bar", void 0),
            oo([m(cc.Node)], t.prototype, "tower_map_node", void 0),
            oo([m(sp.Skeleton)], t.prototype, "tower_boom", void 0),
            oo([p], t)
        );
    })(a.default);
o.default = f;
